﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;

namespace SMART_VIET_V01
{
    public partial class uc_cautraloi : UserControl
    {
        Cursor currentCursor;
        ColorPopup cp;
        public uc_cautraloi()
        {
            InitializeComponent();
            CreateColorPopup(popupControlContainer1);
            for (int i = 8; i <= 50; i += 2)
                repositoryItemRichEditFontSizeEdit1.Items.Add(i);
        }
        #region init
        private FontStyle rtPadFontStyle()
        {
            FontStyle fs = new FontStyle();
            if (iBold.Down)
                fs |= FontStyle.Bold;
            if (iItalic.Down)
                fs |= FontStyle.Italic;
            if (iUnderline.Down)
                fs |= FontStyle.Underline;
            return fs;
        }
        protected void InitFormat()
        {
            iBold.Down = SelectFont.Bold;
            iItalic.Down = SelectFont.Italic;
            iUnderline.Down = SelectFont.Underline;
            iProtected.Down = rtPad.SelectionProtected;
            iBullets.Down = rtPad.SelectionBullet;
            switch (rtPad.SelectionAlignment)
            {
                case HorizontalAlignment.Left:
                    iAlignLeft.Down = true;
                    break;
                case HorizontalAlignment.Center:
                    iCenter.Down = true;
                    break;
                case HorizontalAlignment.Right:
                    iAlignRight.Down = true;
                    break;
            }
        }
        protected void InitPaste()
        {
            iPaste.Enabled = rtPad.CanPaste(DataFormats.GetFormat(0));
        }
        protected Font SelectFont
        {
            get
            {
                if (rtPad.SelectionFont != null)
                    return rtPad.SelectionFont;
                return rtPad.Font;
            }
        }
        protected void RefreshForm(bool b)
        {
            if (b)
            {
                currentCursor = Cursor.Current;
                Cursor.Current = Cursors.WaitCursor;
                Refresh();
            }
            else
            {
                Cursor.Current = currentCursor;
            }
        }

        private void CreateColorPopup(PopupControlContainer container)
        {
            cp = new ColorPopup(container, iFontColor, rtPad);
        }

        #endregion
        private void iCut_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            rtPad.Cut();
            InitPaste();
        }

        private void iCopy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            rtPad.Copy();
            InitPaste();
        }

        private void iPaste_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            rtPad.Paste();
        }

        private void iUndo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            rtPad.Undo();
            iUndo.Enabled = rtPad.CanUndo;
            InitFormat();
        }

        private void iBullets_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            rtPad.SelectionBullet = iBullets.Down;
        }

        private void iFont_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FontDialog dlg = new FontDialog();
            dlg.Font = (Font)SelectFont.Clone();
            dlg.ShowColor = true;
            dlg.Color = rtPad.SelectionColor;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                rtPad.SelectionFont = (Font)dlg.Font.Clone();
                rtPad.SelectionColor = dlg.Color;
            }
        }

        private void iFontColor_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            rtPad.SelectionColor = cp.ResultColor;
        }

        private void fontSize_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void fontSize_ItemPress(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }
        private void iAlign_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (iAlignLeft.Down)
                rtPad.SelectionAlignment = HorizontalAlignment.Left;
            if (iCenter.Down)
                rtPad.SelectionAlignment = HorizontalAlignment.Center;
            if (iAlignRight.Down)
                rtPad.SelectionAlignment = HorizontalAlignment.Right;
        }
        private void iFontStyle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            rtPad.SelectionFont = new Font(SelectFont.FontFamily.Name, SelectFont.Size, rtPadFontStyle());
        }
        public string ContentText { 
            get{
                return this.rtPad.Rtf;
            } 
            set{
                this.rtPad.Rtf = value;
            }
        }

        public bool Checked {
            get { return this.checkBox1.Checked; }
            set { this.checkBox1.Checked = value; }
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}
